<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="1200px">
			<el-form ref="dialogFormRef" :model="state.ruleForm" :rules="state.ruleRules" size="large" label-width="120px">
				 <el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="案例号" prop="caseNo">
							<el-input v-model="state.ruleForm.caseNo" placeholder="请输入案例号" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="案件名" prop="caseName">
							<el-input v-model="state.ruleForm.caseName" placeholder="请输入案件名" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="法院" prop="courtId">
							<el-input v-model="state.ruleForm.courtId" placeholder="请输入法院" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="8" class="mb20">
						<el-form-item label="承办律师" prop="lawId">
							<el-select
							v-model="state.ruleForm.lawId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入承办律师"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option  v-for="item in state.cg_lawlawyer" :key="item.id" :label="item.lawyer" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="8" class="mb20">
						<el-form-item label="当事人类型" prop="entrustType">
							<el-radio-group v-model="state.ruleForm.entrustType" @change="onChangeEntrust()">
                        		<el-radio-button :label="0">企业</el-radio-button>
                        		<el-radio-button :label="1">个人</el-radio-button>
                        	</el-radio-group>
						</el-form-item>
					</el-col>
                    <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="8" class="mb20" id="entrustCompany" v-if="state.ruleForm.entrustType == 1" style="display:none">
						<el-form-item label="企业" prop="linkId">
							<el-select
								v-model="state.ruleForm.companyId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关联企业"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.entrustCompanyListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="8" class="mb20" id="entrustCompany" v-if="state.ruleForm.entrustType == 0">
						<el-form-item label="企业" prop="linkId">
							<el-select
								v-model="state.ruleForm.companyId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关联企业"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.entrustCompanyListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="8" class="mb20" id="entrustPerson"  v-if="state.ruleForm.entrustType == 0" style="display:none">
						<el-form-item label="自然人" prop="linkId">
							<el-select
								v-model="state.ruleForm.personId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关联自然人"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.entrustPersonListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="8" class="mb20" id="entrustPerson"  v-if="state.ruleForm.entrustType == 1">
						<el-form-item label="自然人" prop="linkId">
							<el-select
								v-model="state.ruleForm.personId"
								filterable
								remote
								reserve-keyword
								placeholder="请输入关联自然人"
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.entrustPersonListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="执行标的" prop="subjectMatterMoney">
							<el-input v-model="state.ruleForm.subjectMatterMoney" placeholder="请输入执行标的" clearable></el-input>
						</el-form-item>
					</el-col>
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="回款金额" prop="returnMoney">
							<el-input v-model="state.ruleForm.returnMoney" placeholder="请输入回款金额" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="执行法官" prop="returnMoney">
							<el-select
								v-model="state.ruleForm.judge"
								filterable
								remote
								reserve-keyword
								placeholder="请输入法官"
								multiple
								:remote-method="remoteMethod"
								:loading="loading"
								style="width: 240px"
							>
								<el-option
								v-for="item in state.judgeListData"
								:key="item.id"
								:label="item.name"
								:value="item.id"
								/>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="被执行人" >
							<el-table :data="tableData" style="width: 100%">
								<el-table-column prop="linkId" label="被执行人">
									<template #default="scope">
										<div style="float: left;">
											<el-radio-group v-model="scope.row.linkType" @change="onChangeLink(scope.row)">
												<el-radio-button :label="0">企业</el-radio-button>
												<el-radio-button :label="1">个人</el-radio-button>
											</el-radio-group>
										</div>
										<div :id="'company_'+scope.row.id" v-if="scope.row.linkType == 1" style="float: left; display: none; margin-left: 15px">
											<el-select
												v-model="scope.row.companyId"
												filterable
												remote
												reserve-keyword
												placeholder="请输入关联企业"
												:remote-method="remoteMethod"
												:loading="loading"
												style="width: 240px;"
											>
												<el-option
												v-for="item in state.companyListData"
												:key="item.id"
												:label="item.name"
												:value="item.id"
												/>
											</el-select>
										</div>
										<div :id="'company_'+scope.row.id" v-if="scope.row.linkType == 0" style="float: left; margin-left: 15px">
											<el-select
												v-model="scope.row.companyId"
												filterable
												remote
												reserve-keyword
												placeholder="请输入关联企业"
												:remote-method="remoteMethod"
												:loading="loading"
												style="width: 240px;"
											>
												<el-option
												v-for="item in state.companyListData"
												:key="item.id"
												:label="item.name"
												:value="item.id"
												/>
											</el-select>
										</div>
										<div :id="'person_'+scope.row.id" v-if="scope.row.linkType == 0" style="float: left; display: none; margin-left: 15px">
											<el-select
												v-model="scope.row.personId"
												filterable
												remote
												reserve-keyword
												placeholder="请输入关联自然人"
												:remote-method="remoteMethod"
												:loading="loading"
												style="width: 240px;"
											>
												<el-option
												v-for="item in state.personListData"
												:key="item.id"
												:label="item.name"
												:value="item.id"
												/>
											</el-select>
										</div>
										<div :id="'person_'+scope.row.id" v-if="scope.row.linkType == 1" style="float: left;  margin-left: 15px">
											<el-select
												v-model="scope.row.personId"
												filterable
												remote
												reserve-keyword
												placeholder="请输入关联自然人"
												:remote-method="remoteMethod"
												:loading="loading"
												style="width: 240px;"
											>
												<el-option
												v-for="item in state.personListData"
												:key="item.id"
												:label="item.name"
												:value="item.id"
												/>
											</el-select>
										</div>
									</template>
								</el-table-column>
								<el-table-column prop="lawer" label="对方律师">
									<template #default="scope">
										<el-select v-model="scope.row.lawer" placeholder="请选择" clearable class="mb20">
											<el-option  v-for="item in state.cg_lawlawyer" :key="item.id" :label="item.lawyer" :value="item.id" ></el-option>
										</el-select>
									</template>
								</el-table-column>
							</el-table>
							<el-button type="primary" @click="addPerson()" style="width: 100%;">添加被执行人</el-button>
						</el-form-item>
					</el-col>
                    <!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="8" class="mb20">
						<el-form-item label="案件状态" prop="caseStatus">
							<el-input v-model="state.ruleForm.caseStatus" placeholder="请输入案件状态" clearable></el-input>
						</el-form-item>
					</el-col> -->
                </el-row>

			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="cgcaseForm">
import { reactive, defineAsyncComponent,onMounted, ref ,unref,nextTick} from 'vue';
import { getinfo,save } from '/@/api/cgcase/index';
import {  ElMessage } from 'element-plus';
import { getListAll as getcglawlawyer } from '/@/api/cglaw/index';
import { getListAll as getPersonList } from '/@/api/cgperson/index';
import { getListAll as getCompanyList } from '/@/api/cgcompany/index';
import { getListAll as getEntrustPersonList } from '/@/api/cgentrustperson/index';
import { getListAll as getEntrustCompanyList } from '/@/api/cgentrustcompany/index';
import { getListAll as getJudgeList } from '/@/api/cgjudge/index';


// 引入组件
const Editor = defineAsyncComponent(() => import('/@/components/editor/index.vue'));
const Uploadimg = defineAsyncComponent(() => import('/@/components/Upload/singleImage.vue'));
const Uploadvedio = defineAsyncComponent(() => import('/@/components/Upload/vedio.vue'));

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);


// 定义变量内容
const activeName = ref('first');
const dialogFormRef = ref();
const state = reactive({
	ruleForm: {
		caseNo:'',
		caseName:'',
		courtId:'',
		entrustType:'',
		entrustId:'',
		lawId:'',
		subjectMatterMoney:'',
		returnMoney:'',
		caseStatus:'',
		personList:[],
		judge:[],
		id:'',
	},
	cg_lawlawyer:[],
	judgeListData:[],
	entrustCompanyListData:[],
	entrustPersonListData:[],
	companyListData:[],
	personListData:[],	
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
	// 表单校验
	ruleRules:  { 
    entrustId: [{ required: true, message: '当事人不能为空', trigger: 'blur' }],
    entrustType: [{ required: true, message: '当事人类型不能为空', trigger: 'blur' }],
  },
	loading: false,
	msg:'最佳尺寸100*100',
});
const tableData = ref([])
let id = 0
const addPerson = () => {
	tableData.value.push({
		id:id,
		linkType: 0,
		linkId: 0,
		lawer: 0,
	})
	id = id -1
	console.log(tableData)
}
const onChangeEntrust = () => {
	if(state.ruleForm.entrustType == 0) {
		document.getElementById("entrustCompany").style.display = "";
		document.getElementById("entrustPerson").style.display = "none";
	} else {
		document.getElementById("entrustCompany").style.display = "none";
		document.getElementById("entrustPerson").style.display = "";
	}
};
const onChangeLink = (row) => {
	console.log(row)
	if(row.linkType == 0) {
		document.getElementById("company_" + row.id).style.display = "";
		document.getElementById("person_" + row.id).style.display = "none";
	} else {
		document.getElementById("company_" + row.id).style.display = "none";
		document.getElementById("person_" + row.id).style.display = "";
	}
};
const onUpload = (data) => {
	state.ruleForm.img = data;
};

const onUploadvedio = (data) => {
	state.ruleForm.video = data;
};

// 打开弹窗
const openDialog = (type: string, row?: any) => {
	if (type === 'edit') {
		// 实际请走接口
		getinfo(row.id).then((response: any) => {
			state.ruleForm.caseNo = response.data.caseNo;
			state.ruleForm.caseName = response.data.caseName;
			state.ruleForm.courtId = response.data.courtId;
			state.ruleForm.entrustType = response.data.entrustType;
			state.ruleForm.entrustId = response.data.entrustId;
			if(state.ruleForm.entrustType == 0) {
				state.ruleForm.companyId = response.data.entrustId;
			} else {
				state.ruleForm.personId = response.data.entrustId;
			}
			state.ruleForm.lawId = response.data.lawId;
			state.ruleForm.judge = response.data.judge;
			state.ruleForm.subjectMatterMoney = response.data.subjectMatterMoney;
			state.ruleForm.returnMoney = response.data.returnMoney;
			state.ruleForm.caseStatus = response.data.caseStatus;
			state.ruleForm.id = response.data.id;
			const personList = response.data.personList;
			personList.forEach(function (item, index) {
				if (item.linkType == 0){
					personList[index].companyId =item.linkId
				}	else {
					personList[index].personId =item.linkId
				}
			});
			console.log(response.data.personList);
			tableData.value = response.data.personList;
		});
		
		state.dialog.title = '修改';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			dialogFormRef.value.resetFields();
			initForm();
		});
	}
	getEntrustCompanyList({fileds:['id','name']}).then((response: any) => {
		state.entrustCompanyListData = response.data.data
	});
	getEntrustPersonList({fileds:['id','name']}).then((response: any) => {
		state.entrustPersonListData = response.data.data
	});
	getCompanyList({fileds:['id','name']}).then((response: any) => {
		state.companyListData = response.data.data
	});
	getPersonList({fileds:['id','name']}).then((response: any) => {
		state.personListData = response.data.data
	});
	getJudgeList({fileds:['id','name']}).then((response: any) => {
		state.judgeListData = response.data.data
	});
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	// 查询
	getcglawlawyer({fileds:['id','lawyer']}).then((response: any) => {
		state.cg_lawlawyer = response.data.data;
	});

};
// 关闭弹窗
const closeDialog = () => {
	initForm();
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {

	const formWrap = unref(dialogFormRef) as any;
	if (!formWrap) return;
	formWrap.validate((valid: boolean) => {
		if (valid) {
			state.loading = true;
			if(state.ruleForm.entrustType == 0) {
				state.ruleForm.entrustId = state.ruleForm.companyId;
			} else {
				state.ruleForm.entrustId = state.ruleForm.personId;
			}
			console.log(tableData.value)
			state.ruleForm.personList = tableData.value
			save(state.ruleForm)
				.then(() => {
					ElMessage.success('保存成功');
					state.loading = false;
					closeDialog(); // 关闭弹窗
					emit('refresh');
				})
				.finally(() => {
					state.loading = false;
				});
		}
	})
};
// 页面加载时
onMounted(() => {
	
});


// 表单初始化，方法：`resetFields()` 无法使用
const initForm = () => {
	state.ruleForm.caseNo = '';
	state.ruleForm.caseName = '';
	state.ruleForm.courtId = '';
	state.ruleForm.entrustType = '';
	state.ruleForm.entrustId = '';
	state.ruleForm.companyId = "";
	state.ruleForm.personId = "";
	state.ruleForm.lawId = '';
	state.ruleForm.judge = [];
	state.ruleForm.subjectMatterMoney = '';
	state.ruleForm.returnMoney = '';
	state.ruleForm.caseStatus = '';
	state.ruleForm.personList = [];
	state.ruleForm.id = '';

};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
